<template>
    <div class="article-detail">
        <div class="card-head">
            <h2><span class="iconfont icon-back" @click="goback()"></span>{{category_title}}</h2>
            <div class="placeholder-mide"></div>
        </div>
        <div class="card">
            <div class="card-image" :style="{backgroundImage:`url(${article.thumb_full})`}">
                <img v-if="article.thumb_full" :src="article.thumb_full" alt="">
                <div class="card-description">
                    <h2>{{article.title}}</h2>
                    <p>{{article.description}}</p>
                </div>
            </div>
            <div class="card-content">
                <p class="base-info">发布时间: {{article.puttime|date}}</p>
                <div v-html="article.content"></div>
            </div>
        </div>
    </div>
</template>
<script>
    import NavBar from './Nav-Bar';
    import { router } from '../routes';
    import config, { fetchRest } from '../config';

    export default {
        name: "article-detail",
        components: { NavBar },

        data() {
            this.fetchData();

            var res = {
                article: {},
                loading: true,
                category_title: ""
            }
            return res;
        },
        watch: {
            // 监听路由变动
            '$route': 'fetchData'
        },
        methods: {
            async fetchData() {
                const article_id = router.currentRoute.params.article_id;
                var data = await fetchRest("article?id=" + article_id);
                var article = data[0];
                if (article.thumb) {
                    article.thumb_full = config.api_base_url + article.thumb;
                }

                this.article = article;

                var data = await fetchRest("category?id=" + article.category);
                var category = data[0];
                this.category_title = category.title;
            },
            goback() {
                router.back();
            }
        },
        filters: {
            date(date_str) {
                var d = new Date(date_str);
                return d.getFullYear() + " / " + (d.getMonth() + 1) + " / " + d.getDate();;
            }
        }
    }
</script>
<style scoped>
    .card-head{
        background-color: rgb(77, 182, 172);
        /*height: 86px;*/
        height: 40px;
        overflow: hidden;
        color:#FFF;
        text-align:left;
        padding: 0 16px;
        display: flex;
        /*justify-content:space-between;*/
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 1;
        box-shadow: 0 1px 4px #333;
    }
    .card-head h2{
        margin-top: 10px;
        display: inline-block;
        flex: 1 0 auto;
        font-size:1.3em;
    }
    .card-head .icon-back{
        padding-right: 1em;
        border-right: 1px solid #eee;
        margin-right: 1em;
    }
    .article-detail{
        padding-top: 12px;
        margin-top:40px;
    }
    .card{
        display: block;
        margin: 0px 12px 70px 12px;
        box-sizing: border-box;
        box-shadow: 0px 1px 2px #666;
        border-radius: 2px;
        text-decoration: none;
        color: #333;
    }
    .card .card-image{
        width: 100%;
        overflow: hidden;
        position: relative;
        /*margin: -18px;*/
        background-size:100%;
        background-repeat: no-repeat;
        -webkit-background-size: contain;
        -moz-background-size: contain;
        -o-background-size: contain;
        background-size: contain;
    }
    .card .card-image img{
        width: 100%;
        opacity: 0;
        display: block;
    }
    .card .card-description{
        background-color: rgba(77, 182, 172, 1);
        color:#333;
        text-shadow: 0 0 2px #FFF;
        text-align: left;
        padding: 15px 20px;   
        display: inline-block;
        width: 100%;
        box-sizing: border-box;
    }
    .card .card-image h4{
        margin: 0;
        position: absolute;
        bottom: 16px;
        left: 20px;
        font-size: 15px;
        color: #FFF;
    }
    .card .card-content{
        padding: 20px;
        font-size: 12px;
        text-align: left;
    }
    .card .card-content .base-info{
        text-align: right;
        color: #666;
    }
</style>